<?php

namespace Morrow;

class PageController extends DefaultController {
	public function run() {
		$this->form->loadDef(
			array('myform' => array(

		            "email" => array(
		            	"type" => "text",
		            	"required" => true,
		            	"label" => "email :",
		            	"checktype" => "Email"    
	            	),
            	)
            )
        );
		
		if($this->session->get("uid")){
			$this -> url -> redirect('home');
		}

		$this->form->setInput($this->input->get()); 

		if($this -> form -> isSubmitted('myform')) {
    			$valid = $this -> form -> validate('myform');

    			

            	$this->view->setContent($this->form->getErrors('myform'), "error");

    				if ($valid) {
        		        $formdata = $this -> form -> getValues('myform');

                        $this->load('Db', $this->config->get('db'));
                        //var_dump($formdata);

						$sql = $this->db->result('
							SELECT password
							FROM users
							WHERE email = ?'
							, $formdata['email']);	

						//var_dump($sql);
						//md5 uid

						


						if($sql['NUM_ROWS'] == 0){
							$this->form->setError('myform','email',$this->language->_('邮件地址不存在'));
						}
						else{
							$ps=$sql['RESULT'][0]['password'];
							$email=$formdata['email'];
							
							// create text version of the mail
			                $body = "请复制链接到游览器修改密码:http://youwebapp.com/password/" . $ps . "\n\n";
			                $body .= '你的YouWebapp团队' ;

			                // send mail
			                $this->load('Mail', $this->config->get('mail'));
			                //Debug::dump($this->config->get('mail'));

			                $this->mail->Subject = 'YouWebapp--修改密码';
			                $this->mail->AddAddress($email);
			                $this->mail->Body = $body;
			                $this->mail->Send(true);
			                $this->view->setContent(true, 'contact_success');

			                $this->session->set('messenger', 'emailsend1');
			                $this -> url -> redirect('home');
						}
        				
    				} 
    				else {
    				}
		}

	}
}
